package javafoundations;

/**
 * Created by zhangyunqi on 2017/10/9.
 */

public abstract class ArrayStack<T> implements Stack<T>

{

    private final int DEFAULT_CAPACITY = 10;

    private int count;

    private T[] stack;

    public ArrayStack()

    {

        count = 0;

        stack = (T[])(new Object[DEFAULT_CAPACITY]);

    }

    public T push (T element)

    {

        if (count == stack.length)

            expandCapacity();

        stack[count]=element;

        count++;

        return element;
    }






    private void expandCapacity()

    {

        T[] larger = (T[])(new Object[stack.length*3]);



        for (int index=0; index < stack.length; index++)

            larger[index] = stack[index];

        stack = larger;

    }



    public T pop()throws EmptyCollectionException{

        if(isEmpty())
        count--;
        T result = stack[count];
        stack[count] = null;
        return result;

    }

    public T peek() throws EmptyCollectionException

    {

        if (isEmpty())

            return null;

        else

            return stack[count-1];

    }



    public int size()

    {

        return count;

    }
    public boolean hasNext() {
        int a = -1;
        if(a < count-1)
            return true;
        else
            return false;
    }

}
